<template>
    <!--添加或者修改-->
    <el-dialog
            :title="isAdd?'添加':'编辑'"
            :visible="dialogVisible"
            width="50%"
            :modal-append-to-body='false'
            :show-close="false"
            :before-close="handleClose">
        <div>
            <el-form
                    label-width="100px"
            >
                <#-- ----------  BEGIN 字段循环遍历,表单项  ---------->
                <#list table.fields as field>
                <el-form-item
                        label="${field.comment}"
                >
                    <#if field.propertyType == "Boolean">
                    <el-switch v-model="mForm.${field.propertyName}"/>
                    <#else>
                    <el-input v-model="mForm.${field.propertyName}" <#if field.propertyType== "Integer"||field.propertyType== "Long">type="number"</#if> />
                    </#if>
                </el-form-item>
                </#list>
                <#-- ----------  end 字段循环遍历,表单项  ---------->
            </el-form>
        </div>
        <span slot="footer" class="dialog-footer">
            <el-button @click="close">取 消</el-button>
            <el-button type="primary" :loading="loadingSave" @click="onSave">保 存</el-button>
        </span>
    </el-dialog>
</template>

<script>
    //bean 模板
    const beanTemplate= {
        <#-- ----------  BEGIN 字段循环遍历,bean 模板  ---------->
        <#list table.fields as field>
        ${field.propertyName}: <#if field.propertyType == "Boolean">true<#else>null</#if>,
        </#list>
        <#-- ----------  end 字段循环遍历,bean 模板  ---------->
    };

    export default {
        name: "${entity}ListAdd",
        props: {
        },
        data() {
            return {
                isAdd: true,
                dialogVisible: false,
                loadingSave: false,
                mForm: {},
            };
        },
        methods: {
            /**
             * 获取本页面接口
             **/
            getSelfApi(){
                return this.$apis.admin.${entity?uncap_first};
            },
            close(){
                this.dialogVisible= false;
            },
            /**
             * 显示添加
             * */
            openAdd(){
                this.mForm= {...beanTemplate};
                this.isAdd= true;
                this.dialogVisible= true;
            },
            /**
             * 显示编辑
             * */
            openEdit(bean){
                this.mForm= {...bean};
                this.isAdd= false;
                this.dialogVisible= true;
            },
            /**
             * 处理关闭
             */
            handleClose(){
                // this.dialogVisible= false;
            },
            /**
             * 点击保存
             */
            onSave(){
                let {isAdd} = this;
                let form = {...this.mForm};

                this.loadingSave= true;

                let method = isAdd?'add':'update';

                this.getSelfApi()[method]({
                    bean: form,
                }).then(()=>{
                    this.$message.success('保存成功');
                    this.loadingSave= false;
                    this.close();
                    this.$emit('onSaveSuccess');
                }).catch(()=>{
                    this.loadingSave= false;
                    this.close();
                });
            },
        },
    }

</script>

<style scoped>

</style>